package com.zui.base;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;

import com.opensymphony.xwork2.ActionSupport;
import com.zenith.yasoo.security.User;
import com.zenith.yasoo.security.UserAction;

public class BaseAction extends ActionSupport implements ServletRequestAware, ServletResponseAware {

	private static final long serialVersionUID = 1L;
	/**
	 * HttpServletRequest object
	 */
	protected HttpServletRequest request = null;
	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	/**
	 * HttpServletResponse object
	 */
	protected HttpServletResponse response = null;	
	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}
	
	protected transient final Log log = LogFactory.getLog(getClass());
	
	/**
	 * 得到当前登陆用户
	 * @return
	 */
	public User getCurrentUser() {
		if (request.getSession().getAttribute(ZuiConstants.USER_INFO_IN_SESSION) == null)
			return null;
		return (User)request.getSession().getAttribute(ZuiConstants.USER_INFO_IN_SESSION);

	}
	/**
	 * 得到当前登陆用户的登录用户名
	 * @return
	 */
	public String getCurrentUserName() {
		if(getCurrentUser()!=null)
			return getCurrentUser().getUserName();
		else
			return null;
		
	}
	@SuppressWarnings("unchecked")
	protected void saveMessage(String msg){
		List<String> messages = (List<String>) request.getSession().getAttribute("messages");
        if (messages == null) {
            messages = new ArrayList<String>();
        }
        messages.add( msg );
        request.getSession().setAttribute("messages", messages);
	}

	/**
	 * AJAX 方法
	 * @param xml
	 */
	protected void sampleAjax( String xml ){
		PrintWriter out=null;
		response.setContentType("text/xml; charset=utf-8");
	    response.setHeader("Cache-Control", "no-cache");
	    
	    try{ 
	    	out = response.getWriter();	
	    	out.write( xml );
	    }catch(IOException e){
	    	System.err.println(e.getMessage());     	
	    }finally{
	    	if(out!=null){
	    		out.close();	
	    	}      	
	    }
	}
	
	/************************************/
	
	/**
	 * 得到程序地址
	 * @param request
	 * @return
	 */
	protected String applicationHomePath(HttpServletRequest request){
		StringBuffer url = new StringBuffer();
    	int port = request.getServerPort();
        if (port < 0) {
            port = 80; 
        }
        String scheme = request.getScheme();
        url.append(scheme);
        url.append("://");
        url.append(request.getServerName());
        if ((scheme.equals("http") && (port != 80)) || (scheme.equals("https") && (port != 443))) {
            url.append(':');
            url.append(port);
        }
        url.append(request.getContextPath());
        
        return url.toString();
    }
	
	public HttpServletRequest getRequest() {
		return request;
	}
}